﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GPRSLib;
using System.IO.Ports;
using System.Threading;
using GPRSForm.Send_Interface;
using GPRSForm;
namespace GPRSForm
{
    public partial class Form1 : Form
    {
        SerialPort port = null;
        gmsClass gms = null;
        DataTable datatable = null;
        Database.DataBase database = null;
        AutoSend sendAuto;
        NormalSend sendNormal;
        public Form1()
        {
            InitializeComponent();
            initComponent();
            initDataTable();
            
        }
        public void initDataTable()
        {
            dataGridView_SMS.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            
            datatable = new DataTable("SMS");
            datatable.Columns.Add("STT");
            datatable.Columns.Add("SDT");
            datatable.Columns.Add("Content");
        }
        public void initComponent()
        {
           
            tabSMS.TabPages.Remove(tabReceive);
            tabSMS.TabPages.Remove(tabSend);
            
            #region add port item and set default
            portCbx.Items.Add("COM1");
            portCbx.Items.Add("COM2");
            portCbx.Items.Add("COM3");
            portCbx.Items.Add("COM4");
            portCbx.Items.Add("COM5");
            portCbx.Items.Add("COM6");
            portCbx.SelectedIndex = 2;
            #endregion
            #region add baudrate
            baudRateCbx.Items.Add(4800);
            baudRateCbx.Items.Add(9600);
            baudRateCbx.Items.Add(115200);
            baudRateCbx.SelectedIndex = 2;
            #endregion

        }

        private void ConnectBt_Click(object sender, EventArgs e)
        {
            int baudRate ;
            string portName = null;
            try
            {
                portName = (string)portCbx.SelectedItem;
                baudRate =  (int)baudRateCbx.SelectedItem;
                GlobalClass.dataBaseVar = new Database.DataBase();
                GlobalClass.gmsVar = new gmsClass();
                gms = GlobalClass.gmsVar;
                database = GlobalClass.dataBaseVar;
                port = GlobalClass.gmsVar.OpenPort(portName, baudRate, 8, 300, 300);
                GlobalClass.portVar = port;
                this.tabSMS.TabPages.Add(tabSend);
                this.tabSMS.TabPages.Add(tabReceive);
                this.tabSMS.TabPages.Remove(tabMain);
                initAfterConnect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void initAfterConnect()
        {
            #region add two kind of send
            sendAuto = new AutoSend();
            sendNormal = new NormalSend();
            sendAuto.Size = tabSend.Size;
            sendNormal.Size = tabSend.Size;
            
            sendAuto.Visible = false;
            sendAuto.Enabled = false;
            tabSend.Controls.Add(sendNormal);
            tabSend.Controls.Add(sendAuto);
            #endregion
        }
        
        
        /*
         * method readSMS 
         */
        public DataTable readSMS()
        {
            
            string readOption = null;
            if (radioReadUn.Checked == false && radioReadAll.Checked == true)
            {
                readOption = "AT+CMGL=\"ALL\"";
            }
            else
            {
                readOption = "AT+CMGL=\"REC UNREAD\"";
            }

            List<ShortMessage> messages = gms.ReadSMS(port, readOption);
            foreach (ShortMessage message in messages)
            {
                datatable.Rows.Add(message.Index, message.Sender, message.Message);
            }
            return datatable;
        }

        
       
        
        private void radio_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton radioCurrent = sender as RadioButton;
           
        }

        private void receiveBt_Click(object sender, EventArgs e)
        {
            DataTable datatable = null;
            try
            {
                datatable =  readSMS();
                if (datatable != null)
                {
                    dataGridView_SMS.DataSource = datatable;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (tabSend != null && sendAuto !=null && sendNormal != null)
            {
                sendAuto.Size = tabSend.Size;
                sendNormal.Size = tabSend.Size;
            }
        }

        
    }
}
